package keyboardBehaviors;

import java.awt.AWTEvent;
import java.awt.event.KeyEvent;
import java.util.Enumeration;

import javax.media.j3d.Behavior;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupCondition;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.media.j3d.WakeupOr;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

public class DeleteExecuteBehavior extends Behavior {
	
	private BranchGroup branchToDel = null;
	
	private TransformGroup rooTransformGroup = null;
	
	private WakeupCondition condition = null;
	
	private boolean wakeUp = false;
	
	public DeleteExecuteBehavior(){		
		WakeupOnAWTEvent wakeupOne = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
		WakeupCriterion[] criterion = new WakeupCriterion[1];
		criterion[0] = wakeupOne;
		condition = new WakeupOr(criterion);
	}
	
	public void setBT(BranchGroup b, TransformGroup t){
		this.branchToDel = b;
		this.rooTransformGroup = t;
	}
	
	@Override
	public void initialize() {
		wakeupOn(condition);
	}

	@Override
	public void processStimulus(Enumeration criteria) {
		WakeupOnAWTEvent ev;
        WakeupCriterion genericEvt;
        AWTEvent[] events;
        System.out.println("==================");
        while (criteria.hasMoreElements()) 
        {
            genericEvt = (WakeupCriterion) criteria.nextElement();

            if ((genericEvt instanceof WakeupOnAWTEvent)&&(wakeUp == true)) 
            {
                ev = (WakeupOnAWTEvent) genericEvt;
                events = ev.getAWTEvent();
                for (int n = 0; n < events.length; n++){
        			if (events[n] instanceof KeyEvent){
        				KeyEvent eventKey = (KeyEvent) events[n];
        				int keyCode = eventKey.getKeyCode();                   
                        if(keyCode == KeyEvent.VK_C){
                        	rooTransformGroup.removeChild(branchToDel);
                        	
                        }                    
                        
        			}
        		}
                wakeUp = false;
            }
        }
        wakeupOn(condition);
	}
	
	
	public void wakeUp(){
		this.wakeUp = true;
	}
}
